<div class="layui-card">
	<div class="layui-card-header">
		<h2 class="header-title">用户列表</h2>
		<span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>用户列表</cite></a>
        </span>
	</div>
	<div class="layui-card-body zbyulist">
		<div class="layui-row layui-col-space20">
			<div class="layui-col-xs6 layui-col-md6">
				<div class="layui-form toolbar">
					<label>查询方式</label>
					<div class="layui-input-inline">
						<select name="lxz" id="userSele" lay-verify="required">
							<option value="userId">ID搜索</option>
							<option selected="selected" value="nickname">昵称搜索</option>
							<option value="username">手机号搜索</option>
						</select>
					</div>
					<input id="queryUser" class="layui-input search-input" type="text" placeholder="请输入" />&emsp;
					<button id="logr-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
				</div>
			</div>
			<div class="layui-col-xs3 layui-col-md3 layui-col-md-offset3">
				<div class="layui-btn-group demoTable" style="">
					<button class="layui-btn" data-type="getCheckData" style="float:right">添加群组成员</button>
					<button class="layui-btn layui-btn-normal" data-type="getTui" style="float:left;margin-right: 8px!important;">推送消息</button>
					<i class="layui-clear"></i>
				</div>
			</div>
			<div class="layui-clear"></div>
		</div>
		<div class="layui-btn-container">
			<button id="userasc" class="layui-btn">用户ID<div id="usera" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="healthasc" class="layui-btn">健康<div id="healtha" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="orangeasc" class="layui-btn">脏腑<div id="zfa" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="spiritasc" class="layui-btn">精神<div id="jsa" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="sportasc" class="layui-btn">运动<div id="yda" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="sleepasc" class="layui-btn">睡眠<div id="sma" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
			<button id="qdasc" class="layui-btn">渠道<div id="qda" class="zbyicon"><i class="layui-icon layui-icon-triangle-d"></i></div></button>
		</div>

		<table class="layui-table" id="logr-table" lay-filter="demo"></table>
	</div>
</div>
<script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-primary layui-btn-sm zbyck" title="查看详情" lay-event="detail"><i class="layui-icon layui-icon-layer"></i></a>
	<a class="layui-btn layui-btn-sm zbygt" title="沟通" lay-event="edit"><i class="layui-icon layui-icon-dialogue"></i></a>
	<a class="layui-btn layui-btn-sm layui-btn-normal zbygt" title="修改客服" lay-event="editqx"><i class="layui-icon layui-icon-edit"></i></a>
</script>
<script>
	layui.use(['laydate', 'table', 'util', 'config', 'index', 'admin'], function() {
		var laydate = layui.laydate;
		var table = layui.table;
		var config = layui.config;
		var util = layui.util;
		var com = layui.common;
		var index = layui.index;
		var admin = layui.admin;

		var idRand = 1
		var timeRank = 1
		var allRank = 1
		var sleepRank = 1
		var sportRank = 1
		var spiritRank = 1
		var visceraRank = 1

		function showTable(tabledata) {
			table.render({
				elem: '#logr-table',
				id: 'idTest',
				data: tabledata,
				page: true,
				limit: 20,
				response: {
					countName: 'data.total' //数据总数的字段名称，默认：count
				},
				cols: [
					[{
							checkbox: true
						},
						{
							field: 'headPortrait',
							title: '头像',
							width: 80,
							style: 'height:70px;padding:0;',
							templet: function(b) {
								if(b.headPortrait == null || b.headPortrait == "") {
									var s = '<div><img src="assets/images/mrtx.png" width="50px"  height="50px" /></div>'
									return s
								} else {
									var s = '<div><img src="'
									s += b.headPortrait;
									s += '" width="50px"  height="50px" /></div>'
									return s
								}
							}
						},
						{
							field: 'userId',
							title: 'ID',
							width: 70
						},
						{
							field: 'nickname',
							title: '用户昵称',
							width: 100,
							templet: function(b) {
								if(b.nickname != null) {
									if(b.nickname.length == 1) {
										return b.nickname
									} else if(b.nickname.length == 2) {
										var a = b.nickname;
										a = a.substring(0, 1)
										return a + "*"
									} else {
										if(b.nickname != null) {
											var a = b.nickname;
											var x = a.substring(0, 1)
											var c = a.substring(b.nickname.length - 1, b.nickname.length)
											return x + "***" + c
										}
									}
								} else {
									return "未填写"
								}
							}
						},
						{
							field: 'sex',
							title: '性别',
							width: 80
						},
						{
							field: 'birthday',
							title: '年龄',
							width: 80,
							templet: function(b) {
								if(b.birthday != null) {
									return birthday(b.birthday) + "岁";
								} else {
									return "未填写"
								}
							}
						},
						{
							field: 'groups',
							title: '群组'
						},
						{
							field: 'scoreSprite',
							title: '健康数据',
							width: 175,
							templet: function(b) {
								//timeSleep	timeSport timeSprite timeViscera timeAll
								var a0 = "",
									a1 = "",
									a2 = "",
									a3 = "",
									a4 = "",
									a5 = "";
								if(b.continueAll) {
									a0 = b.continueAll
								} else {
									a0 = ""
								}
								if(b.timeAll) {
									if(b.scoreAll) {
										a5 = b.scoreAll
									} else {
										a5 = ""
									}
								}

								if(b.timeSprite) {
									if(b.scoreSprite) {
										a1 = b.scoreSprite
									} else {
										a1 = ""
									}
								}

								if(b.timeViscera) {
									if(b.scoreViscera) {
										a2 = b.scoreViscera
									} else {
										a2 = ""
									}
								}

								if(b.timeSleep) {
									if(b.scoreSleep) {
										a3 = b.scoreSleep
									} else {
										a3 = ""
									}
								}

								if(b.timeSport) {
									if(b.scoreSport) {
										a4 = b.scoreSport
									} else {
										a4 = ""
									}
								}

								var a = "<table><tr>" +
									"<td> " + healthNums(b.scoreAll, b.timeAll) + "</td>" +
									"<td>" + userSirit(b.scoreSprite, b.timeSprite) + "</td>" +
									"<td>" + userOrgan(b.scoreViscera, b.timeViscera) + "</td>" +
									"<td>" + userSleep(b.scoreSleep, b.timeSleep) + "</td>" +
									"<td>" + userSport(b.scoreSport, b.timeSport) + "</td>" +
									"<td> " + jianc(b.continueAll) + "</td>" +
									"<tr/>" +
									"<tr style='text-align: center;'>" +
									"<td>" + a5 + "</td>" +
									"<td>" + a1 + "</td>" +
									"<td>" + a2 + "</td>" +
									"<td>" + a3 + "</td>" +
									"<td>" + a4 + "</td>" +
									"<td> " + a0 + "</td>" +
									"<tr/>" +
									"</table>"
								return a
								//								return +userSirit(b.scoreSprite) + userOrgan(b.scoreViscera) + userSleep(b.scoreSleep) + userSport(b.scoreSport) + '<br/>' +
								//									a0 + a1 + a2 + a3 + a4
							}
						},
						{
							field: 'orgName',
							title: '渠道',
							templet: function(d) {
								if(d.orgName == null) {
									return "松果健康";
								} else {
									return d.orgName
								}
							}
						},
						{
							field: 'customerName',
							//							title: '<div class="layui-input-inline"><select name="lxz" id="userSele" lay-verify="required"><option value="userId">ID搜索</option><option selected="selected" value="nickname">昵称搜索</option><option value="username">手机号搜索</option></select></div>'
							title: '客服人员'
						}, {
							field: 'caozuo',
							title: '操作',
							toolbar: "#barDemo"
						}
					]
				]
			});
			var kid = admin.getTempData("yigeid")
			if(kid.rows.customerServiceId == null) {
				$('.zbygt').css("display", "none");
			}
		}

		function userListV(uasc, quer, usc) {
			var alog = config.getTempData('loginInfo');
			var a;
			if(alog.orgId == 1) {
				a = ""
			} else {
				a = alog.orgId + ""
			}
			layer.load(2);

			var uid = "";
			var niname = "";
			var uname = ""

			if(usc == 'username') {
				uname = quer
			} else if(usc == 'nickname') {
				niname = quer
			} else if(usc == 'userId') {
				uid = quer
			}
			var s1 = uasc.split(" ")[0];
			s1 = s1.split('.')[1]
			s1 = s1.split('+')[0]

			if(s1 == 'user_id') {
				s1 = ''
			}
			if(s1 == 'org_id') {
				s1 = ''
			}
			console.log(s1)

			$.ajax({
				type: "POST",
				url: userListPath,
				data: {
					params: JSON.stringify({
						sortOrder: uasc,
						sortOrder1: s1,
						orgId: a,
						pageSize: 1000,
						pageNumber: 1,
						nickname: niname,
						userId: uid,
						username: uname,
					})
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function(request) {},
				success: function(data) {
					layer.closeAll('loading');
					showTable(data.rows);
				},
				beforeSend: function() {},
				complete: function() {},

				error: function(result) {
					console.log(result);
					config.removeToken();
					location.replace('login.html');
				},
			});
		}

		//搜索按钮点击事件
		$('#logr-btn-search').click(function() {
			var us = $("#userSele ").val();
			var searchAccount = $('#queryUser').val().trim();
			userListV("u.user_id asc", searchAccount, us);
		});

		$('#userasc').click(function() {
			if(idRand == 1) {
				idRand = 0;
				$('#usera').addClass("zbyicon2");
				$("#usera").removeClass("zbyicon");
				userListV("u.user_id desc", null, null);
			} else {
				idRand = 1;
				$('#usera').addClass("zbyicon");
				$("#usera").removeClass("zbyicon2");
				userListV("u.user_id asc", null, null);
			}
			closeOther(1, timeRank, allRank, sleepRank, sportRank, spiritRank, visceraRank);
		})

		$('#healthasc').click(function() {
			if(allRank == 1) {
				allRank = 0;
				recordstate = "so.score_all+0 desc";
				$('#healtha').addClass("zbyicon2");
				$("#healtha").removeClass("zbyicon");
				userListV("so.score_all+0 desc", null, null);
			} else {
				allRank = 1;
				recordstate = "sh.score_all+0 asc";
				$('#healtha').addClass("zbyicon");
				$("#healtha").removeClass("zbyicon2");
				userListV("so.score_all+0 asc", null, null);
			}
			closeOther(idRand, timeRank, 1, sleepRank, sportRank, spiritRank, visceraRank);
		})

		$('#orangeasc').click(function() {
			if(visceraRank == 1) {
				visceraRank = 0;
				recordstate = "so.score_viscera+0 desc";
				$('#zfa').addClass("zbyicon2");
				$("#zfa").removeClass("zbyicon");
				userListV("so.score_viscera+0 desc", null, null);
			} else {
				visceraRank = 1;
				recordstate = "so.score_viscera+0 asc";
				$('#zfa').addClass("zbyicon");
				$("#zfa").removeClass("zbyicon2");
				userListV("so.score_viscera+0 asc", null, null);
			}
			closeOther(idRand, timeRank, allRank, sleepRank, sportRank, spiritRank, 1);
		})

		$('#sleepasc').click(function() {
			if(sleepRank == 1) {
				sleepRank = 0;
				recordstate = "so.score_sleep+0 desc";
				$('#sma').addClass("zbyicon2");
				$("#sma").removeClass("zbyicon");
				userListV("so.score_sleep+0 desc", null, null);
			} else {
				sleepRank = 1;
				recordstate = "so.score_sleep+0 asc";
				$('#sma').addClass("zbyicon");
				$("#sma").removeClass("zbyicon2");
				userListV("so.score_sleep+0 asc", null, null);
			}
			closeOther(idRand, timeRank, allRank, 1, sportRank, spiritRank, visceraRank);
		})

		$('#spiritasc').click(function() {
			if(spiritRank == 1) {
				spiritRank = 0;
				recordstate = "so.score_sprite+0 desc";
				$('#jsa').addClass("zbyicon2");
				$("#jsa").removeClass("zbyicon");
				userListV("so.score_sprite+0 desc", null, null);
			} else {
				spiritRank = 1;
				recordstate = "so.score_sprite+0 asc";
				$('#jsa').addClass("zbyicon");
				$("#jsa").removeClass("zbyicon2");
				userListV("so.score_sprite+0 asc", null, null);
			}
			closeOther(idRand, timeRank, allRank, sleepRank, sportRank, 1, visceraRank);
		})

		$('#sportasc').click(function() {
			if(sportRank == 1) {
				sportRank = 0;
				recordstate = "so.score_sport+0 desc";
				$('#yda').addClass("zbyicon2");
				$("#yda").removeClass("zbyicon");
				userListV("so.score_sport+0 desc", null, null);
			} else {
				sportRank = 1;
				recordstate = "so.score_sport+0 asc";
				$('#yda').addClass("zbyicon");
				$("#yda").removeClass("zbyicon2");
				userListV("so.score_sport+0 asc", null, null);
			}
			closeOther(idRand, timeRank, allRank, sleepRank, 1, spiritRank, visceraRank);
		})

		$('#qdasc').click(function() {
			if(timeRank == 1) {
				timeRank = 0;
				recordstate = "u.org_id desc";
				$('#qda').addClass("zbyicon2");
				$("#qda").removeClass("zbyicon");

				userListV("u.org_id desc", null, null);
			} else {
				timeRank = 1;
				recordstate = "u.org_id asc";
				$('#qda').addClass("zbyicon");
				$("#qda").removeClass("zbyicon2");
				userListV("u.org_id asc", null, null);
			}
			closeOther(idRand, 1, allRank, sleepRank, sportRank, spiritRank, visceraRank);
		})

		function closeOther(idRands, timeRanks, allRanks, sleepRanks, sportRanks, spiritRanks, visceraRanks) {

			if(idRands == 0) {
				idRand = 1;
				$('#usera').addClass("zbyicon");
				$("#usera").removeClass("zbyicon2");
			}
			if(timeRanks == 0) {
				timeRank = 1;
				$('#qda').addClass("zbyicon");
				$("#qda").removeClass("zbyicon2");
			}
			if(allRanks == 0) {
				allRank = 1;
				$('#healtha').addClass("zbyicon");
				$("#healtha").removeClass("zbyicon2");
			}
			if(sleepRanks == 0) {
				sleepRank = 1;
				$('#sma').addClass("zbyicon");
				$("#sma").removeClass("zbyicon2");
			}
			if(sportRanks == 0) {
				sportRank = 1;
				$('#yda').addClass("zbyicon");
				$("#yda").removeClass("zbyicon2");
			}
			if(spiritRanks == 0) {
				spiritRank = 1;
				$('#jsa').addClass("zbyicon");
				$("#jsa").removeClass("zbyicon2");
			}
			if(visceraRanks == 0) {
				visceraRank = 1;
				$('#zfa').addClass("zbyicon");
				$("#zfa").removeClass("zbyicon2");
			}
		}

		var userinfoId;
		//监听表格复选框选择
		table.on('checkbox(demo)', function(obj) {
			//			console.log(obj)
		});
		//监听工具条
		table.on('tool(demo)', function(obj) {
			var data = obj.data;
			if(obj.event === 'detail') {
				//				layer.msg('ID：' + data.userId + ' 的查看操作');
				admin.putTempData(data.sungoId + "u", data); // 用缓存传递参数

				if(data.nickname != null) {
					if(data.nickname.length == 1) {} else if(data.nickname.length == 2) {
						var a = data.nickname;
						a = a.substring(0, 1)
						data.nickname = a + "*"
					} else {
						if(data.nickname != null) {
							var a = data.nickname;
							var x = a.substring(0, 1)
							var c = a.substring(data.nickname.length - 1, data.nickname.length)
							data.nickname = x + "***" + c
						}
					}
				} else {
					data.nickname = "未填写"
				}

				// 不分离版本用后台地址
				index.openNewTab({
					title: data.nickname,
					url: 'components/system/userinfo.html',
					menuId: data.sungoId + "u"
				});

			} else if(obj.event === 'edit') {

				var kid = admin.getTempData("yigeid")
				if(kid.rows.customerServiceId == data.customerServiceId) {
					admin.liaotian(data.userId, data.nickname)
				} else {
					layer.alert("您不是此用户的客服人员，请您手动设置！")
				}
			} else if(obj.event === 'editqx') {

				admin.putTempData('ysus', data);
				var title = '修改客服';
				admin.popupCenter({
					title: title,
					path: 'components/system/updateys.html',
					finish: function() {
						userListV("u.user_id asc", null, null);
					}
				});
			}
		});

		showUI()

		function showUI() {
			var alog = config.getTempData('loginInfo');
			$.ajax({
				type: "POST",
				url: infosUser,
				data: {
					userId: alog.userId
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					console.log(data.rows)
					admin.putTempData('yigeid', data);
					userListV("u.user_id asc", null, null);
				},
				error: function(result) {
					console.log(result);
				},
			});
		}

	});

	function btnShow() {

	}

	function birthday(strBirthday) {
		if(strBirthday != null) {

			var returnAge;
			var strBirthdayArr = strBirthday.split("-");
			var birthYear = strBirthdayArr[0];
			var birthMonth = strBirthdayArr[1];
			var birthDay = strBirthdayArr[2];

			d = new Date();
			var nowYear = d.getFullYear();
			var nowMonth = d.getMonth() + 1;
			var nowDay = d.getDate();

			if(nowYear == birthYear) {
				returnAge = 0; //同年 则为0岁
			} else {
				var ageDiff = nowYear - birthYear; //年之差
				if(ageDiff > 0) {
					if(nowMonth == birthMonth) {
						var dayDiff = nowDay - birthDay; //日之差
						if(dayDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					} else {
						var monthDiff = nowMonth - birthMonth; //月之差
						if(monthDiff < 0) {
							returnAge = ageDiff - 1;
						} else {
							returnAge = ageDiff;
						}
					}
				} else {
					returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
				}
			}

			return returnAge; //返回周岁年龄

		}
	}

	function healthNums(nums, g) {
		if(g) {
			if(nums != null) {
				return '<img class="userScore" title="健康分值：' + nums + '" src="assets/images/jingshen_songguo1@2x.png" />'
			} else {
				return ''
			}
		} else {
			return ''
		}

	}

	function jianc(nums) {
		if(nums != null) {
			return '<img class="userScore" title="持续天数：' + nums + '" src="assets/images/calendar.png" />'
		} else {
			return ''
		}
	}

	function userSleep(sleep, g) {
		if(g) {
			if(sleep != null) {
				return '<img class="userScore" title="睡眠分值：' + sleep + '" src="assets/images/u555.png" />'
			} else {
				return ''
			}
		} else {
			return ''
		}

	}

	function userOrgan(organ, g) {
		if(g) {
			if(organ != null) {
				return '<img class="userScore" title="脏腑分值：' + organ + '" src="assets/images/u552.png" />'
			} else {
				return ''
			}
		} else {
			return ''
		}

	}

	function userSport(sport, g) {
		if(g) {
			if(sport != null) {
				return '<img class="userScore" title="运动分值：' + sport + '" src="assets/images/u553.png" />'
			} else {
				return ''
			}
		} else {
			return ''
		}
	}

	function userSirit(sirit, g) {
		if(g) {
			if(sirit != null) {
				return '<img class="userScore" title="精神分值：' + sirit + ' " src="assets/images/u551.png" />'
			} else {
				return ''
			}
		} else {
			return ''
		}

	}
	layui.use(['table', 'index', 'admin'], function() {
		var table = layui.table;
		var index = layui.index;
		var admin = layui.admin;
		var $ = layui.$,
			active = {
				getCheckData: function() { //获取选中数据
					var checkStatus = table.checkStatus('idTest'),
						data = checkStatus.data;
					admin.putTempData('selectUser', data);
					var title = '添加群组人员';
					admin.popupCenter({
						title: title,
						path: 'components/system/addGroupUser.html',
						finish: function() {
							userListV("u.user_id asc", null, null);
						}
					});
				},
				getTui: function() {
					var checkStatus = table.checkStatus('idTest'),
						data = checkStatus.data;
					admin.putTempData('selectUser2', data);
					var title = '推送消息';
					admin.popupCenter({
						title: title,
						path: 'components/system/userMsg.html',
						finish: function() {
							userListV("u.user_id asc", null, null);
						}
					});
				}
			};

		$('.demoTable .layui-btn').on('click', function() {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
	});
</script>